package com.zhibang.controller;

import com.zhibang.entity.Employee;
import com.zhibang.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@CrossOrigin("*")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
     public @ResponseBody String addEmployee(@RequestBody Employee employee){
        Employee employee1=new Employee();
        employee1.setEname(employee.getEname());
        employee1.setSex(employee.getSex());
        employee1.setAge(employee.getAge());
        employee1.setPosts(employee.getPosts());
        return employeeService.save(employee1)?"添加成功":"添加失败";
    }
    // 查询全部员工
    @GetMapping("/employees")
    public @ResponseBody List<Employee> getAllEmployees() {
        return employeeService.list();
    }

    // 根据员工ID查询员工信息
    @GetMapping("/employee/{eid}")
    public @ResponseBody Employee getEmployeeById(@PathVariable Integer eid) {
        return employeeService.getById(eid);
    }

    // 根据姓名进行模糊查询
    @GetMapping("/employee/search")
    public @ResponseBody List<Employee> searchEmployeeByName(@RequestParam String ename) {
        return employeeService.lambdaQuery()
                .like(Employee::getEname, ename)
                .list();
    }

    // 修改员工信息（根据员工ID）
    @PutMapping("/employee")
    public @ResponseBody String updateEmployee(@RequestBody Employee employee) {
        boolean result = employeeService.updateById(employee);
        return result ? "修改成功" : "修改失败";
    }

    // 删除单个员工（根据员工ID）
    @DeleteMapping("/employee/{eid}")
    public @ResponseBody String deleteEmployee(@PathVariable Integer eid) {
        boolean result = employeeService.removeById(eid);
        return result ? "删除成功" : "删除失败";
    }

    // 批量删除员工（传入员工ID列表）
    @DeleteMapping("/employees")
    public @ResponseBody String deleteMultipleEmployees(@RequestBody List<Integer> eids) {
        boolean result = employeeService.removeBatchByIds(eids);
        return result ? "批量删除成功" : "批量删除失败";
    }
 }
                                                                                  